package com.useeinfo.oa.open.api;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.AppVersionBiz;
import com.useeinfo.oa.modules.base.entity.AppVersion;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/appVersion")
public class AppVersionAPI {

    @Autowired
    private AppVersionBiz appVersionBiz;

    /**
     * 获取版本号
     *
     * @param deviceType   设备类型
     * @param surroundings 环境
     * @return
     */
    @RequestMapping(value = "/getAppVersion")
    public ResultDto getAreaList(@RequestParam(value = "deviceType", required = false) String deviceType,
                                 @RequestParam(value = "surroundings", required = false) String surroundings,
                                 @RequestParam(value = "versionCode", required = false) String versionCode

                                 ) {
        if (StringUtils.isBlank(deviceType)) {
            return ResultUtil.error("设备类型不能为空");
        }
        if (StringUtils.isBlank(surroundings)) {
            return ResultUtil.error("环境不能为空");
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("deviceType", deviceType);
        queryParam.getSqlMap().put("surroundings", surroundings);
        queryParam.getSqlMap().put("versionCode", versionCode);
        List<AppVersion> appVersionList = appVersionBiz.findList(queryParam);
        if (CollectionUtils.isEmpty(appVersionList)) {
            return ResultUtil.success(new JSONObject());
        }
        return ResultUtil.success(appVersionList.get(0).toJSONObject());
    }
}
